package com.scud.gateway.config;

import com.scud.gateway.filter.JwtFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.SecurityWebFiltersOrder;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.context.ServerSecurityContextRepository;

@EnableWebFluxSecurity
@Configuration
public class GatewaySecurityConfig {

    @Autowired
    private JwtFilter jwtFilter;


    @Bean
    public SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
        http.authorizeExchange()
                .pathMatchers("/api/system/index/login",
                        "/api/system/index/code/*",
                        "/api/product/**",
                        "/api/system/manage/index/login",
                        "/api/system/manage/index/register").permitAll()
                .anyExchange().authenticated()
                .and().csrf().disable()
                .httpBasic();
        http.addFilterAt(jwtFilter,SecurityWebFiltersOrder.HTTP_BASIC);
        return http.build();
    }

}
